<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="驿站名称" prop="title">
        <el-input v-model="pram.title" maxlength="150" placeholder="驿站名称" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="pram.status" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="pram.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="驿站位置" prop="address">
        {{pram.address}}
        <el-button size="mini" icon="el-icon-location" type="primary" @click="showChoosePosition=true">点击选择驿站位置</el-button>
      </el-form-item>
      <el-form-item label="介绍" prop="content">
        <Tinymce v-model="pram.content"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:post:update','admin:post:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
      <el-dialog title="选择位置" v-if="showChoosePosition" :visible.sync="showChoosePosition" width="40%" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" top="5vh" append-to-body>
        <iframe v-if="showChoosePosition" id="choosePosition" style="width: 100%;height: 500px" frameborder=0 src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=ZILBZ-FEDCL-6M3PO-M7GUW-PXP5T-ABFB4&referer=零工超市">
        </iframe>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" class="primary"  @click="showChoosePosition = false">确 定</el-button>
          </span>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import * as postApi from '@/api/post.js'
import {getToken} from "@/utils/auth";
import {Debounce} from '@/utils/validate'
import Tinymce from '@/components/Tinymce/index.vue'
export default {
  // name: "edit"
  components: {Tinymce },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
  },
  data() {
    return {
      constants:this.$constants,
      showChoosePosition:false,
      pram: {
        title: '',
        sort: 0,
        status:1,
        content:'',
        address:'',
        latitude:null,
        longitude:null,
        id: null,
      },
      rules: {
        title: [{ required: true, message: '请填驿站名称', trigger: ['blur', 'change'] }],
        content: { required: true, message: '请填写介绍', trigger: ['blur'] },
        address: { required: true, message: '请选择位置', trigger: ['blur'] },
      },
      myHeaders: { "X-Token": getToken() },
      localFile: null,
    }
  },

  beforeDestroy() {
    window.removeEventListener('message',function (){

    })
  },
  mounted() {
    this.initEditData()
    window.addEventListener('message', (event) =>{
      // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
      let loc = event.data;
      if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
        this.pram.latitude=loc.latlng.lat;
        this.pram.longitude=loc.latlng.lng;
        this.pram.address=loc.poiaddress
      }
    }, false);
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {

      if (this.isCreate !== 1) return //编辑
      const { title,sort,status,content,id,address } = this.editData
      this.pram.title = title
      this.pram.sort = sort
      this.pram.status = status
      this.pram.content = content
      this.pram.address = address
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      postApi.postAdd(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      postApi.postAdd(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped lang="scss">
.imgDel{
  position: absolute;
  z-index: 4;
  top: 1px;
  border: 0;
  width: 15px!important;
  height: 15px!important;
  text-align: center;
  //right: -1px;
  left:64px;
  padding: 0px!important;
  line-height: 10px!important;
  border-radius: 0!important;
  font-weight:bold;
}
.img-box{
  position:absolute;
  z-index: 3;
  top:0;
  width: 80px;
  height: 80px;
  border:0;
}
</style>
